package io.renren.modules.sys.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.modules.sys.entity.SysDeptEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.ContainerEntity;
import io.renren.modules.sys.entity.OrdertrackingEntity;
import io.renren.modules.sys.service.ContainerService;
import io.renren.modules.sys.service.OrdertrackingService;
import io.renren.modules.sys.shiro.ShiroUtils;

/**
 * 订单跟踪表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-11-26 23:59:51
 */
@RestController
@RequestMapping("sys/ordertracking")
public class OrdertrackingController {
	@Autowired
	private OrdertrackingService	ordertrackingService;
	@Autowired
	private ContainerService		containerService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:ordertracking:list")
	public R list(@RequestParam Map<String, Object> params) {

		PageUtils page = ordertrackingService.queryPage(params);

		return R.ok().put("page", page);
	}

	// https://blog.csdn.net/weixin_30512027/article/details/83270344
	/**
	 * 信息
	 */
	@RequestMapping("/info/{number}")
	@RequiresPermissions("sys:ordertracking:info")
	public R info(@PathVariable("number") Integer number) {
		OrdertrackingEntity ordertracking = ordertrackingService.selectById(number);
		Wrapper<ContainerEntity> wrapper = new EntityWrapper<ContainerEntity>();
		// QueryWrapper<ContainerEntity> queryWrapper = new QueryWrapper<>();
		// wrapper = new Condition();
		wrapper.eq("order_id", number);
		List<ContainerEntity> containerList = containerService.selectList(wrapper);
		return R.ok().put("ordertracking", ordertracking).put("containerList", containerList);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:ordertracking:save")
	public R save(@RequestBody OrdertrackingEntity ordertracking) {
		String businessCode = (String) ShiroUtils.getSession().getAttribute("businesscode");
		if (businessCode != null && businessCode.length() > 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String str = sdf.format(new Date());
			String waybill = businessCode + "-" + str;
			ordertracking.setWaybill(waybill);
		}
		ordertracking.setCreateUserId(ShiroUtils.getUserId());
		ordertracking.setDeptId(ShiroUtils.getUserEntity().getDeptId());
		ordertrackingService.insert(ordertracking);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:ordertracking:update")
	public R update(@RequestBody OrdertrackingEntity ordertracking) {
		if (ordertracking.getWaybill() == null || ordertracking.getWaybill().length() == 0) {
			String businessCode = (String) ShiroUtils.getSession().getAttribute("businesscode");
			if (businessCode != null && businessCode.length() > 0) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				String str = sdf.format(new Date());
				String waybill = businessCode + "-" + str;
				ordertracking.setWaybill(waybill);
			}
		}
		//判断录入费用，用车状态为完结状态receiptFreight
		if(ordertracking.getReceiptFreight() != null && ordertracking.getReceiptFreight() != 0)
		{
			Wrapper<ContainerEntity> wrapper = new EntityWrapper<ContainerEntity>();
			wrapper.eq("order_id", ordertracking.getId());
			List<ContainerEntity> containerList = containerService.selectList(wrapper);
			for(ContainerEntity c : containerList){
				c.setState("完结");
			}
			containerService.updateBatchById(containerList);
		}
		ordertrackingService.updateById(ordertracking);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:ordertracking:delete")
	public R delete(@RequestBody Integer[] numbers) {
		ordertrackingService.deleteBatchIds(Arrays.asList(numbers));

		return R.ok();
	}

}
